package com.situ.ftbserver.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.ftbserver.model.Coach;
import com.situ.ftbserver.model.search.CoachSearchBean;
import org.apache.ibatis.annotations.*;
import org.mybatis.caches.caffeine.CaffeineCache;

import java.util.List;
import java.util.Map;



@Mapper

public interface CoachMapper extends BaseMapper<Coach> {
    default Page<Coach> findAll(Page<Coach> page, CoachSearchBean csb) {
        LambdaQueryWrapper<Coach> wrapper = Wrappers.lambdaQuery(csb);
        if (csb.getStartDate() != null) {
            wrapper.ge(Coach::getBirthday, csb.getStartDate());
        }
        if (csb.getEndDate() != null) {
            wrapper.lt(Coach::getBirthday, csb.getEndDate());
        }
        return this.selectPage(page, wrapper);
    }

    @Select("SELECT ch.name, COUNT(r.id) AS count " +
            "FROM coach ch " +
            "JOIN course c ON ch.id = c.coach_id " +
            "LEFT JOIN reserve r ON c.id = r.course_id " +
            "GROUP BY ch.id " +
            "ORDER BY count DESC " +
            "LIMIT 5;")
    List<Map<String, Integer>> getPopularCoach();


}
